// match.controller.ts
import {
  Controller,
  Get,
  Query,
  Post,
  Body,
  Request,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { MatchService } from './matches.service';

@Controller('matches')
export class MatchController {
  constructor(private readonly matchService: MatchService) {}

  // 根据用户 ID 和偏好条件查询匹配
  @UseGuards(AuthGuard('jwt'))
  @Get()
  async getMatches(@Request() req, @Query('preferences') preferences: string) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.matchService.findMatches(
        userId,
        JSON.parse(preferences || '{}'),
      );
      return {
        success: true,
        data: result,
        message: '获取匹配列表成功',
      };
    } catch (error) {
      return {
        success: false,
        data: [],
        message: '获取匹配列表失败: ' + error.message,
      };
    }
  }

  // 处理滑动操作（喜欢/不喜欢）
  @UseGuards(AuthGuard('jwt'))
  @Post('swipe')
  async handleSwipe(
    @Request() req,
    @Body() body: { targetUserId: number; action: string },
  ) {
    try {
      const userId = parseInt(req.user.id);
      const { targetUserId, action } = body;
      const result = await this.matchService.handleSwipe(
        userId,
        targetUserId,
        action,
      );
      return {
        success: true,
        data: result,
        message: '操作成功',
      };
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '操作失败: ' + error.message,
      };
    }
  }

  // 获取匹配列表
  @UseGuards(AuthGuard('jwt'))
  @Get('list')
  async getMatchList(@Request() req) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.matchService.getUserMatches(userId);
      return {
        success: true,
        data: result,
        message: '获取匹配列表成功',
      };
    } catch (error) {
      return {
        success: false,
        data: [],
        message: '获取匹配列表失败: ' + error.message,
      };
    }
  }

  // 获取新匹配列表
  @UseGuards(AuthGuard('jwt'))
  @Get('new')
  async getNewMatches(@Request() req) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.matchService.getNewMatches(userId);
      return {
        success: true,
        data: result,
        message: '获取新匹配列表成功',
      };
    } catch (error) {
      return {
        success: false,
        data: [],
        message: '获取新匹配列表失败: ' + error.message,
      };
    }
  }

  // 保存用户筛选条件
  @UseGuards(AuthGuard('jwt'))
  @Post('filters')
  async saveFilters(@Request() req, @Body() filters: any) {
    try {
      const userId = parseInt(req.user.id);
      await this.matchService.saveUserFilters(userId, filters);
      return {
        success: true,
        message: '筛选条件保存成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '筛选条件保存失败: ' + error.message,
      };
    }
  }

  // 获取用户筛选条件
  @UseGuards(AuthGuard('jwt'))
  @Get('filters')
  async getFilters(@Request() req) {
    try {
      const userId = parseInt(req.user.id);
      const filters = await this.matchService.getUserFilters(userId);
      return {
        success: true,
        data: filters,
        message: '获取筛选条件成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '获取筛选条件失败: ' + error.message,
      };
    }
  }
}
